import 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'
// import NProgress from 'nprogress'
// import 'nprogress/nprogress.css'
import routes from './routes'
// 本地模拟api
// import Mock from './mock'
// Mock.bootstrap()

import 'font-awesome/css/font-awesome.min.css'

Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)

// NProgress.configure({ showSpinner: false })

const router = new VueRouter({
  mode: 'history', // 这行是去掉url后面的#
  routes
})

router.beforeEach((to, from, next) => {
  // 设置token, 避免刷新丢失token
  if (localStorage.getItem('token')) {
    store.commit('SETTOKEN', localStorage.getItem('token'))
  }
  // NProgress.start()
  if (to.path === '/login') {
    sessionStorage.removeItem('user')
  }
  let user = JSON.parse(sessionStorage.getItem('user'))
  if (!user && to.path !== '/login') {
    next({path: '/login'})
  } else {
    // 设置登录之后返回的路径
    if (to.fullPath !== '/login') {
      store.commit('SETRETURNPATH', to.fullPath)
    }
    next()
  }
})
// router.afterEach(transition => {
// NProgress.done()
// })

new Vue({
  // el: '#app',
  // template: '<App/>',
  router,
  store,
  // components: { App }
  render: h => h(App)
}).$mount('#app')
